export function hashCode(str) {
  let hash = 0;
  if (str.length === 0) {
    return hash;
  }
  for (let i = 0; i < str.length; i++) {
    let char = str.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    // Convert to 32bit integer
    hash = hash & hash;
  }
  return hash;
}

function Rad(d) {
  return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
}

export function getDistance(lat1, lng1, lat2, lng2) {
  let radLat1 = Rad(lat1);
  let radLat2 = Rad(lat2);
  let a = radLat1 - radLat2;
  let b = Rad(lng1) - Rad(lng2);
  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
      Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137;// EARTH_RADIUS;
  s = Math.round(s * 10000) / 10; //输出为m
  return s;
}

export function getUrlQueryString(url, name) {
  url = decodeURIComponent(url)
  let reg = `(^|&)${name}=([^&]*)(&|$)`;
  let r = url.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}
export function yuan(price) {
  if (price % 100 === 0) {
    return price / 100;
  }
  if (price % 10 === 0) {
    return (price / 100).toFixed(1);
  }
  return (price / 100).toFixed(2);
}

//防抖
export function throttle(fn,wait) {
  let time = wx.getStorageSync("throttleTime")||null ;
  let context = this;
  let args = arguments;
  if(new Date().getTime() - time>wait){
    wx.setStorageSync("throttleTime",new Date().getTime())
    fn.apply(context, args);
  }else {
    wx.setStorageSync("throttleTime",new Date().getTime())
  }
};
//节流
export function debounce(fn, wait) {
  let timer = wx.getStorageSync("debounceFn")||null ;
  return function () {
    let context = this;
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
    timer = setTimeout(function () {
      fn.apply(context, args)
    }, wait)
    wx.setStorageSync("debounceFn",timer)
  }
}


// 上面的节流和防抖概念搞错了，而且要频繁操作storage io操作
/**
 * @desc 防抖 在最后执行一次
 * @param { Function } cb 回调函数 required
 * @param { Number } wait 等待时间 required
 * @param { Boolean } immediate 是否立即执行一次
 */
 export function _debounce(cb, wait = 1000, immediate = false) {
  if(!cb || 'function' !== typeof cb) throw new TypeError('cb不能为空或cb必须是一个函数');
  let timer = null;
  return function() {
      if(timer) clearTimeout(timer);
      if(immediate && !timer) {
          cb.apply(this, arguments);
      }
      timer = setTimeout(() => {
          cb.apply(this, arguments);
          clearTimeout(timer);
          timer = null;
      }, wait);
  }
}

export const PaymentOptions = {
  1: "微信支付",
  2: "余额支付",
  3: "支付宝",
  4: "银联支付",
  5: "现金",
}

export const BizCodeOptions = [
  {
    key: 9001,
    label: "会员充值",
    usePay: true,
    features: "COMMON",
    isConsume: false,
  },
  {
    key: 9002,
    label: "会员开卡",
    usePay: true,
    features: "COMMON",
    isConsume: false,
  },
  {
    key: 9003,
    label: "赠送会员",
    usePay: true,
    features: "COMMON",
    isConsume: false,
  },
  {
    key: 9004,
    label: "预订",
    usePay: true,
    features: "BOOKING",
    isConsume: true
  },
  {
    key: 9005,
    label: "微点餐",
    usePay: true,
    features: "WCANYIN",
    isConsume: true
  },
  {
    key: 9006,
    label: "商家助手小程序",
    usePay: true,
    features: "COMMON",
    isConsume: true
  },
  {
    key: 9007,
    label: "校园缴费",
    usePay: true,
    features: "SCHOOL_PAY",
    isConsume: true
  },
  {
    key: 9008,
    label: "POS机APP",
    usePay: true,
    features: "COMMON",
    isConsume: true
  },
  {
    key: 9009,
    label: "军博游乐",
    usePay: true,
    features: "JUNBO",
    isConsume: true
  },
  {
    key: 9010,
    label: "会员年费缴纳",
    usePay: true,
    features: "COMMON",
    isConsume: true
  },
  {
    key: 9011,
    label: "积分商城",
    usePay: true,
    features: "COMMON",
    isConsume: true
  },
  {
    key: 9012,
    label: "码牌支付",
    usePay: true,
    features: "COMMON",
    isConsume: true
  },
  {
    key: 9050,
    label: "推广奖励",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9051,
    label: "会员导入",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9052,
    label: "人工客服",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9053,
    label: "充值赠送",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9054,
    label: "升级赠送",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9055,
    label: "卡过期清零",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9070,
    label: "扫码领取优惠券",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9071,
    label: "营销赠送",
    usePay: false,
    features: "COMMON",
    isConsume: false
  },
  {
    key: 9100,
    label: "开放平台",
    usePay: true,
    features: "COMMON",
    isConsume: true
  }
];
export function weekNumStringToChinese(data) {
  /**
   * data=1,2,3,4,5,6,7 需要按照排序对应周日到周六
   */
  let str = "";
  let weekOptions = {
    1: "周日",
    2: "周一",
    3: "周二",
    4: "周三",
    5: "周四",
    6: "周五",
    7: "周六"
  };
  if (data === "1,7") {
    str = "周六日";
  } else if (data === "2,3,4,5,6") {
    str = "周一到周五";
  } else if (data === "1,2,3,4,5,6,7") {
    str = "周一到周日";
  } else {
    let arr = data.split(",");
    if (arr[0] == 1) {
      arr.splice(0, 1);
      arr.push("1");
    }
    arr.forEach(num => {
      str += weekOptions[num];
    });
  }
  return str;
}

export const MALL_RESERVATION_SHELF_ROUTER_PATH = {
  MALL_SHELF:'mall/goods-detail',
  RESERVATION_SHELF:'reservation/project_detail'
}

export const emailTest = (value) =>{
  const reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$")
  return reg.test(value)
}

export const phoneTest = (value) =>{
  const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
  return reg.test(value)
}

export const numberTest = (value) =>{
  const reg = /^\d+$/
  return reg.test(value)
}

export const numOrEnTest = (value) =>{
  const reg = /^[a-z0-9]+$/i
  return reg.test(value)
}
export const checkMobile = (tel) => {
  var phoneReg = !!tel.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/);
  var telReg = !!tel.match(/^([0-9]{3,4}-)?[0-9]{7,8}$/);
  console.log(phoneReg, telReg);
  if (phoneReg || telReg) {
      return true;
  } else {
      return false;
  }
}

